package com.lyshop.jinxiang.activitys;

import android.util.Log;
import android.view.View;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.adapters.FundRecordAdapter;
import com.lyshop.jinxiang.application.MyApplication;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.TransfeRecordBean;
import com.lyshop.jinxiang.net.ActionCallbackListener;

import java.util.ArrayList;
import java.util.List;

public class FundRecordActivity extends BaseActivity implements View.OnClickListener, OnRefreshListener, OnLoadMoreListener {

    private static final String TAG = "FundRecordActivity";
    private RecyclerView rv_list;
    private SmartRefreshLayout refreshLayout;

    private FundRecordAdapter adapter;

    private  int page = 1;
    private  String  type ="1";
    private List<TransfeRecordBean> list = new ArrayList<>();
    RadioButton rb_join,rb_out;
    @Override
    protected int setContentView() {
        return R.layout.activity_fund_record;
    }

    @Override
    protected void findViews() {
        refreshLayout = (SmartRefreshLayout) $(R.id.refresh_layout);
        rv_list = (RecyclerView) $(R.id.rv_list);
        refreshLayout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        rv_list = findViewById(R.id.rv_list);
        adapter = new FundRecordAdapter(mContext, list);
        ((SimpleItemAnimator) rv_list.getItemAnimator()).setSupportsChangeAnimations(false);
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        rv_list.setAdapter(adapter);
        rb_join = (RadioButton)$(R.id.rb_join);
        rb_out = (RadioButton)$(R.id.rb_out);
        loadViewData();
    }

    @Override
    protected void setListeners() {
        rb_join.setOnClickListener(this);
        rb_out.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.rb_join){
            type = "1";
            loadViewData();
            return;
        }
        if(view.getId() == R.id.rb_out){
            type = "2";
            loadViewData();
            return;
        }
    }
    private void loadViewData(){
        rv_list.setVisibility(View.GONE);
        refreshLayout.finishRefresh(2000);
        refreshLayout.finishLoadMore(2000);
        mAppAction =  MyApplication.getApplication().getAppAction();
        mAppAction.getTransfeRecord(type, page, new ActionCallbackListener<List<TransfeRecordBean>>() {
            @Override
            public void onSuccess(List<TransfeRecordBean> data) {
                Log.e(TAG, "onSuccess: ."+data.toString());
                rv_list.setVisibility(View.VISIBLE);
                if (page == 1){
                    list.clear();
                }
                list.addAll(data);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(String errorCode, String message) {

            }
        });

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        loadViewData();
        refreshLayout.finishRefresh(2000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page ++;
        loadViewData();
        refreshLayout.finishRefresh(2000);
    }
}
